package com.dgw.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtil implements ApplicationContextAware {
	
	// Spring应用上下文环境  
    private static ApplicationContext applicationContext;  
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtil.applicationContext=applicationContext;
	}
	
	//获取spring上下文
	public static ApplicationContext getApplicationContext() {  
        return applicationContext;  
    }  
	
	//获取spring容器中的bean
	@SuppressWarnings("unchecked")
	public static <T>T getBean(String beanId) throws BeansException {  
        return (T)applicationContext.getBean(beanId);  
	}  
	
}
